//
//  VIZTabBar.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/7.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit

class VIZTabBar: UITabBar {


    //重写init方法
    override init(frame: CGRect) {
        //注意顺序
        super.init(frame: frame)
        self.addSubview(plusButton)
        bringSubviewToFront(plusButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK 懒加载plusButton
    var plusButton :UIButton = {

        let plus = UIButton()
        
        //plus.backgroundColor = UIColor.redColor()
        //设置中间按钮的背景图
        plus.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        plus.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        plus.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        plus.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        
        return plus
        
}()

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.frame.width/5
        let hight = self.bounds.height
        
        var index:CGFloat = 0
        
        for subview in subviews{
           
            //如果没有类型可以先编译一下显示类型
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subview.frame = CGRect(x: index * width, y: 0, width: width, height: hight)
                
                index += 1
                
                if index == 2 {
                    
                    plusButton.frame =  CGRect(x: index * width, y: 0, width: width, height: hight)
                    
                    index = 3
                }
                //三目运算符不行啊???
//                index == 1 ? 2 : index
                //print(index)
                
            }
           
        }
        
    }
    
}
